/* ******************************************************************************
 * Copyright Notice:
 * Copyright (c) Huawei Technologies Co., Ltd. 2022-2022. ALL Rights Reserved.
 *
 * Warning: This computer software sourcecode is protected by copyright law
 * and international treaties. Unauthorized reproduction or distribution
 * of this sourcecode, or any portion of it, may result in severe civil and
 * criminal penalties, and will be prosecuted to the maximum extent
 * possible under the law.
 * *******************************************************************************
 */

package android.app;

import android.content.Context;
import android.content.SharedPreferences;

import java.io.File;
import java.util.HashSet;
import java.util.Set;

/**
 * {@hide}
 */
public class SharedPreferencesHelper {

    private SharedPreferencesImpl mPreferences;
    private File mFile;

    public SharedPreferencesHelper(File file) {
        mFile = file;
        mPreferences = new SharedPreferencesImpl(mFile, Context.MODE_PRIVATE);
    }

    public SharedPreferences getSharedPreferences() {
        return mPreferences;
    }

    public void changeMode(int mode) {
        if (mFile.exists()) {
            ContextImpl.setFilePermissionsFromMode(mFile.getPath(), mode, 0);
        }
    }

    public String getString(String key, String defValue) {
        return mPreferences.getString(key, defValue);
    }

    public boolean putString(String key, String value) {
        SharedPreferences.Editor editor = mPreferences.edit();
        editor.putString(key, value);
        return editor.commit();
    }

    public boolean putString(String[] keys, String value) {
        SharedPreferences.Editor editor = mPreferences.edit();
        for (String key : keys) {
            editor.putString(key, value);
        }
        return editor.commit();
    }

    public int getInt(String key, int defValue) {
        return mPreferences.getInt(key, defValue);
    }

    public boolean putInt(String key, int value) {
        SharedPreferences.Editor editor = mPreferences.edit();
        editor.putInt(key, value);
        return editor.commit();
    }

    public boolean putInt(String[] keys, int value) {
        SharedPreferences.Editor editor = mPreferences.edit();
        for (String key : keys) {
            editor.putInt(key, value);
        }
        return editor.commit();
    }

    public boolean remove(String key) {
        SharedPreferences.Editor editor = mPreferences.edit();
        editor.remove(key);
        return editor.commit();
    }

    public Set<String> getStringSet(String key, Set<String> set) {
        return mPreferences.getStringSet(key, set);
    }

    public boolean putStringSet(String key, Set<String> set) {
        SharedPreferences.Editor editor = mPreferences.edit();
        editor.putStringSet(key, new HashSet<>(set));
        return editor.commit();
    }
}
